#include <hl7/util.hpp>
#include <hl7/except.hpp>

int
main(int argc, char **argv)
{
    using namespace std;
    using namespace hl7;

    ifstream file;
    ifstream::pos_type size;

    vector<string> lines;
    vector<string> fields;
    vector< vector<string> > sgmt;
    string message;

    file.open("../data/example_msg.hl7", ios::in|ios::binary|ios::ate);

    if(file.is_open())
    {
        size = file.tellg();
        char *msg;
        msg = new char[(unsigned int) size];
        file.seekg(0, ios::beg);
        file.read(msg, size);
        file.close();
        message = msg;
        delete [] msg;

        try
        {
            using namespace util;
            lines = split(message, "\r", lines);

            for(vector<string>::iterator line = lines.begin(); line != lines.end(); line++)
            {
                fields = util::split((*line), "|", fields);
                if(fields.size() > 0)
                    sgmt.push_back(fields);

                fields.clear();
            }

            cout << "segments:"<<endl;

            for(vector< vector<string> >::iterator iter = sgmt.begin(); iter != sgmt.end(); iter++)
                cout << iter->front() << endl;

#if 0
            //wtf is wrong with this?
            string d = "\n";
            string foo = join(lines,d);
#endif
        }
        catch(Exception &e)
        {
            cout << e.what() << endl;
            return EXIT_FAILURE;
        }
    }

    return EXIT_SUCCESS;
}
